from django.http import Http404, HttpResponseRedirect, HttpResponseServerError
from django.utils.translation import ugettext as _
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.template.loader import get_template
from django.core import urlresolvers
from django.core.mail import EmailMessage
from django.contrib.sites.models import Site
from django.views.generic.list_detail import object_detail, object_list
from django.conf import settings
from faq.models import *
from faq.forms import QuestionForm

def question_list(request,
				template_name = 'faq/question_list.html',
				template_form = 'faq/question_form.html',
				topic = None,
				**kwargs):
	form = QuestionForm()
	extra_context = {'form': form, 'template_form': template_form}
	
	if topic:
		topic = get_object_or_404(Topic, slug = topic)
		questions = Question.objects.published(topic = topic)
		extra_context['current_topic'] = topic
	else:
		questions = Question.objects.published()
	
	topics = Topic.objects.published()
	extra_context['topics'] = topics
	
	return object_list(
		request,
		queryset = questions,
		template_name = template_name,
		extra_context = extra_context,
		**kwargs
	)

def question_form(request,
				template_name = 'faq/question_form_view.html',
				template_form = 'faq/question_form.html',
				template_asked_verify = 'faq/question_asked_verify_email.html',
				redirect_to = '/faq/confirm/',
				**kwargs):
	if request.method == 'POST':
		form = QuestionForm(request.POST)
		if form.is_valid():
			pending_question = form.save()
			
			# send activation URL to the submitted email address for confirmation
			template = get_template(template_asked_verify)
			current_site = Site.objects.get_current()
			context = {
				'pending_question': pending_question,
				'current_site': current_site,
				'domain': current_site.domain,
				'name': current_site.name,
				'url': pending_question.get_absolute_url()
			}
			body = template.render(RequestContext(request, context))
			subject = '%s %s' % (_('Please verify your question:'), pending_question.question)
			recipients = [pending_question.email]
			msg = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)
			msg.content_subtype = 'html'
			msg.send()
			
			return HttpResponseRedirect(redirect_to)
	else:
		form = QuestionForm()
	return render_to_response(template_name, {'form': form, 'template_form': template_form}, context_instance = RequestContext(request))

def pending_question_verify(request,
							code = None,
							template_asked = 'faq/question_asked_email.html',
							template_confirmed = 'faq/question_confirmed_email.html',
							redirect_to = '/faq/verified/',
							**kwargs):
	pending_question = get_object_or_404(PendingQuestion, activation_code = code)
	current_site = Site.objects.get_current()
	
	# send admin link to alertees for review, answering, and publication
	if not settings.DEBUG:
		template = get_template(template_asked)
		context = {
			'pending_question': pending_question,
			'pending_question_admin_url': urlresolvers.reverse('admin:faq_question_change', args = (pending_question.id,)),
			'pending_question_name': pending_question.name,
			'domain': current_site.domain,
			'name': current_site.name
		}
		body = template.render(RequestContext(request, context))
		subject = '%s %s' % (_('FAQ question asked by'), pending_question.name)
		recipients = [mail_tuple[1] for mail_tuple in settings.ALERTEES]
		msg = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)
		msg.content_subtype = 'html'
		msg.send()
	
	# send confirm message to the submitted email address
	template = get_template(template_confirmed)
	context = {
		'pending_question': pending_question,
		'current_site': current_site,
		'domain': current_site.domain,
		'name': current_site.name
	}
	body = template.render(RequestContext(request, context))
	subject = '%s %s' % (_('Thank you. Your question was received:'), pending_question.question)
	recipients = [pending_question.email]
	msg = EmailMessage(subject, body, settings.DEFAULT_FROM_EMAIL, recipients)
	msg.content_subtype = 'html'
	msg.send()
	
	pending_question.delete()

	return HttpResponseRedirect(redirect_to)
